package com.zyh.aspect;

import com.zyh.annotation.LogAnnotation;
import com.zyh.entity.Log;
import com.zyh.service.ILogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;

@Aspect
@Component
public class LogAspect {
    @Autowired
    ILogService logService;

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("@annotation(com.zyh.annotation.LogAnnotation)")
    public void pointcut(){}

    @After("pointcut()")
    public void logAfter(JoinPoint joinPoint) throws NoSuchMethodException {
//        logger.info("开始采集日志");
//        //获取当前系统时间
//        Date now = new Date();
//        //获取方法名
//        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
//        Method method = signature.getMethod();
//        String methodName = method.getName();
//        //获取方法实参列表
//        Object[] args = joinPoint.getArgs();
//
//        //通过日志输出信息
//        logger.info("当前系统时间：{}，进入到方法：{}，传入的参数：{}",now,methodName,args);
        //获取用户名
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        String username = (String)requestAttributes.getAttribute("username",RequestAttributes.SCOPE_SESSION);
        //获取系统时间
        LocalDateTime now = LocalDateTime.now();
        //获取用户登录的ip
        ServletRequestAttributes httpRequestAttributes = (ServletRequestAttributes)requestAttributes;
        HttpServletRequest request = httpRequestAttributes.getRequest();
        String ip = request.getRemoteHost();
        //获取操作类型和操作内容

        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();
        System.out.println("methodName = " + methodName);

        MethodSignature methodSignature = (MethodSignature)signature;
        Class[] parameterTypes = methodSignature.getParameterTypes();
        System.out.println("parameterTypes = " + Arrays.toString(parameterTypes));

        Method method = joinPoint.getTarget().getClass().getDeclaredMethod(methodName,parameterTypes);
        System.out.println("method = " + method);
        LogAnnotation annotation = method.getDeclaredAnnotation(LogAnnotation.class);
        System.out.println("annotation = " + annotation);
        String type = annotation.type();
        String content = annotation.content();
        //将以上数据封装为Log实体对象，并调用LogService的addLog方法
        Log log = new Log();
        log.setUsername(username);
        log.setLogDate(now);
        log.setLogIp(ip);
        log.setLogType(type);
        log.setLogContent(content);
        logService.save(log);
    }
}
